package com.example.day1.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.day1.common.R;
import com.example.day1.entity.Employee;
import com.example.day1.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")//json格式上传，所以要配置注解
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1.获取密码,利用md5进行加密
        String password = employee.getPassword();


        //2.根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Employee> QueryWrapper = new LambdaQueryWrapper<>();
        QueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(QueryWrapper);
        if(emp==null){
            return R.error("登录失败");
        }

        //3.进行数据库密码匹配比对，如果不同就返回
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }

        //4.判断用户的权限状态
        if(emp.getStatus()!=1){
            return R.error("账号已经禁用");
        }

        //5.放入Session对象中
        HttpSession session = request.getSession();
        session.setAttribute("employee",emp.getId());

        return R.success(emp);


    }
    @RequestMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request){
        //添加用户登录所在数据库中保留的信息
        employee.setPassword("123456");
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//       Long CreateUser= (Long)request.getSession().getAttribute("employee");
//        employee.setCreateUser(CreateUser);
//        employee.setUpdateUser(CreateUser);
        //存放入数据库
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    @RequestMapping("/page")
    //获取请求参数
    public R<Page> page(int page,int pageSize,String name){
        log.info("page:"+page+" pageSize:"+pageSize+" name:"+name);
        Page page1 = new Page(page, pageSize);

        LambdaQueryWrapper<Employee> QueryWrapper = new LambdaQueryWrapper<>();
        //如果查询条件有name就在进行一个匹配
        QueryWrapper.like(name!=null,Employee::getName,name);
        //做一个排序
        QueryWrapper.orderByDesc(Employee::getUpdateTime);
        Page page2 = employeeService.page(page1, QueryWrapper);
        return R.success(page2);

    }

    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpServletRequest request){

        log.info(String.valueOf(employee.getId()));
        employee.setUpdateTime(LocalDateTime.now());
        Long UpdateUser= (Long)request.getSession().getAttribute("employee");
        employee.setUpdateUser(UpdateUser);

        employeeService.updateById(employee);
        return R.success("修改信息成功");

    }

    /**
     *
     * @param id
     * @return编辑员工信息
     */
    @GetMapping("/{id}")
    public R<Employee> getByID(@PathVariable Long id){
        log.info("根据id查询员工信息");
        Employee byId = employeeService.getById(id);
        if(byId==null){
            return R.error("没有查询到员工信息");
        }
        return R.success(byId);
    }





}
